package bean;

import java.util.ArrayList;

public class HomeData {
    private String keyword;
    private Integer sort;
    private int curPage;
    private int totalElement;
    private int numPerPage=10;
    private int totalPages;
    private int navigationPages=5;
    private int firstPageForNavigation;
    private int lastPageForNavigation;
    private ArrayList<Commodity> commodityList;
    public HomeData(String keyword,int sort,int curPage){
        this.keyword=keyword;
        this.sort=sort;
        this.curPage=curPage;
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public int getCurPage() {
        return curPage;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    public int getTotalElement() {
        return totalElement;
    }

    public void setTotalElement(int totalElement) {
        this.totalElement = totalElement;
//        this.firstPageForNavigation=1;
//        this.lastPageForNavigation=(totalElement/numPerPage)+1;
        this.totalPages = ((totalElement-1)/numPerPage)+1;

        int firstPage=1;
        int lastPage=totalElement/numPerPage+1;

        if (totalPages<=navigationPages){
            this.firstPageForNavigation=1;
            this.lastPageForNavigation=lastPage;
        }else if(curPage<=navigationPages/2){
            this.firstPageForNavigation=firstPage;
            this.lastPageForNavigation=navigationPages;
        }else if(curPage>=lastPage-navigationPages/2){
            this.firstPageForNavigation=lastPage-navigationPages;
            this.lastPageForNavigation=lastPage;
        }else{
            this.firstPageForNavigation=curPage-navigationPages/2;
            this.lastPageForNavigation=curPage+navigationPages/2;
        }

        this.totalPages = ((totalElement-1)/numPerPage)+1;
    }

    public int getNumPerPage() {
        return numPerPage;
    }

    public void setNumPerPage(int numPerPage) {
        this.numPerPage = numPerPage;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
//        this.totalPages = ((totalElement-1)/numPerPage)+1;
    }

    public int getNavigationPages() {
        return navigationPages;
    }

    public void setNavigationPages(int navigationPages) {
        this.navigationPages = navigationPages;
    }

    public int getFirstPageForNavigation() {
        return firstPageForNavigation;
    }

    public void setFirstPageForNavigation(int firstPageForNavigation) {
//        this.firstPageForNavigation = firstPageForNavigation;
        if (totalElement==0){
            this.firstPageForNavigation=0;
        }else if(curPage<navigationPages/2){
            this.firstPageForNavigation = firstPageForNavigation;
        }
    }

    public int getLastPageForNavigation() {
        return lastPageForNavigation;
    }

    public void setLastPageForNavigation(int lastPageForNavigation) {
        this.lastPageForNavigation = lastPageForNavigation;
    }

    public ArrayList<Commodity> getCommodityList() {
        return commodityList;
    }

    public void setCommodityList(ArrayList<Commodity> commodityList) {
        this.commodityList = commodityList;
    }

    @Override
    public String toString() {
        return "HomeData{" +
                "keyword='" + keyword + '\'' +
                ", sort=" + sort +
                ", curPage=" + curPage +
                ", totalElement=" + totalElement +
                ", numPerPage=" + numPerPage +
                ", totalPages=" + totalPages +
                ", navigationPages=" + navigationPages +
                ", firstPageForNavigation=" + firstPageForNavigation +
                ", lastPageForNavigation=" + lastPageForNavigation +
                ", commodityList=" + commodityList +
                '}';
    }
}
